
import UIKit
#if canImport(TCC_String)
import TCC_String
#endif
#if canImport(TCC_Extension)
import TCC_Extension
#endif
#if canImport(TCC_Const)
import TCC_Const
#endif
@objcMembers public class CCTabBarController: UITabBarController {
    private var item_layer_arr = [CAShapeLayer]()
    public override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        setupColor()
        
    }
    
    /// 设置tabbar的字体颜色，背景颜色
    func setupColor(){
        //tabBar.isTranslucent = false
        tabBar.tintColor = "#FFFFFF".color()
        tabBar.unselectedItemTintColor = "#000000".color()
        let bgcolor = "#161718".color()
        if #available(iOS 13, *) {
            let appearance = self.tabBar.standardAppearance.copy()
            //appearance.shadowImage = UIImage.withColor(bgcolor)///设置阴影
            appearance.shadowColor = bgcolor/////设置阴影
            appearance.shadowImage = UIImage.withColor(bgcolor)
            appearance.backgroundColor = "#FFFFFF".color(0.0)
            appearance.backgroundImage = UIImage(named: "bg_tabbar")
            appearance.backgroundEffect = nil
            self.tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                ///用这个方法的，这个一定要加，否则15.0系统下会出问题，一滑动tabbar就变透明!!!
                self.tabBar.scrollEdgeAppearance = appearance
            } else {}
        }else{
            tabBar.shadowImage = UIImage.withColor(bgcolor)
            tabBar.backgroundColor = bgcolor
            tabBar.backgroundImage = UIImage.withColor(bgcolor)
        }
    }
    
    func setupViewController()
    {
        let controllers = [UIViewController()]
        let titles = ["name"]
        let imgs = ["img"]
        let selectImgs = ["img_select"]
        for i in 0..<controllers.count{
            let vc = controllers[i]
            vc.tabBarItem = UITabBarItem()
            //vc.title = titles[i]
            vc.tabBarItem.title = titles[i]
            // vc.tabBarItem.image = UIImage(named:imgs[i])!
            //vc.tabBarItem.selectedImage = UIImage(named:selectImgs[i])!
            vc.tabBarItem.image = UIImage(named:imgs[i])?.withRenderingMode(.alwaysOriginal)
            vc.tabBarItem.selectedImage = UIImage(named:selectImgs[i])?.withRenderingMode(.alwaysOriginal)
            let nav = CCNavigationController(rootViewController:vc)
            nav.navigationBar.isHidden = false
            self.addChild(nav)
        }
    }
    
    /// 设置tabbar的layer，绘制一些特殊layer的tabbar
    func setupLayer(){
        ///这里的效果是 <设置tabbar，中间凸出半圆的layer>
    }
    
}
// MARK: - TabBar特殊效果`1`，tabbar，中间凸出半圆的layer
@objc public extension CCTabBarController{
    func setTabbarMidRadiusLayer(){
        let bounds = CGRect(x: 0, y: 0, width: kUIScreenWidth, height: tabBar.bounds.height + 40)
        let r : CGFloat = 12
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: r))
        path.addArc(withCenter: CGPoint(x: r, y: r),
                    radius: r,
                    startAngle: .pi,
                    endAngle: .pi * 3.0 / 2.0,
                    clockwise: true)
        path.addArc(withCenter: CGPoint(x: kUIScreenWidth / 2.0, y: 10),
                    radius: (50 + 13) / 2 - 2,
                    startAngle: .pi - 0.092 * .pi,
                    endAngle: 0.092 * .pi,
                    clockwise: true)
        path.addLine(to: CGPoint(x: kUIScreenWidth - r, y: 0))
        path.addArc(withCenter: CGPoint(x: kUIScreenWidth - r, y: r),
                    radius: r,
                    startAngle: .pi * 3.0 / 2.0,
                    endAngle: 0,
                    clockwise: true)
        path.addLine(to: CGPoint(x: 0, y: bounds.height))
        path.addLine(to: CGPoint(x: 0, y: r))
        
        let layer = CAShapeLayer()
        layer.frame = bounds
        layer.path = path.cgPath
        layer.fillColor = "#FFFFFF".color().cgColor
        
        tabBar.layer.addSublayer(layer)
    }
}


// MARK: - TabBar特殊效果`2`，tabbar，带圆角，上下左右都有偏移的距离
@objc public extension CCTabBarController{
    ///1，
    func setupTabbarRadiusLayer(){
        let offX : CGFloat = 10; //ayer，左右偏移距离
        let offY : CGFloat = 10; //layer, 上下便宜距离
        let bounds = CGRect(x:0, y: 0, width: kUIScreenWidth - offX * 2.0, height: self.tabBar.bounds.size.height)
        let path = UIBezierPath(roundedRect: bounds, cornerRadius: 10)
        
        let layer = CAShapeLayer()
        layer.frame = CGRect(x:offX, y: 0, width: bounds.width, height: bounds.width)
        layer.path = path.cgPath
        layer.fillColor = "#FFFFFF".color().cgColor
        self.tabBar.layer.addSublayer(layer)
        
        ///每个item的背景layer，选中是黑色，未选中是白色
        let item_offx = CGFloat(5)
        let item_offy = CGFloat(5)
        let width = CGFloat(54)
        let item_bounds = CGRect(x:0, y: 0, width:width, height:36)
        let gap = (bounds.width - item_offx  * 2 - item_bounds.width * 4) / 3
        for i in 0..<4{
            let item_path = UIBezierPath(roundedRect: item_bounds, cornerRadius: 10)
            let item_layer = CAShapeLayer()
            item_layer.frame = CGRect(x:(gap + width) * CGFloat(i) + offX + item_offx  , y: offY + 2.0, width: item_bounds.width, height: item_bounds.height)
            item_layer.path = item_path.cgPath
            item_layer.fillColor = (i == 0 ? "#000000" : "#FFFFFF").color().cgColor
            self.tabBar.layer.addSublayer(item_layer)
            self.item_layer_arr.append(item_layer)
        }
    }
    ///2。需要在实现下面的tabbarcontroller的delegate方法 的 `tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)`调用下面这个方法
    internal func didSelect(){
        for i in 1..<self.tabBar.subviews.count{
            let layer = self.item_layer_arr[i-1]
            layer.fillColor = self.selectedIndex + 1 == i ? "#000000".color().cgColor : "#FFFFFF".color().cgColor
        }
    }
}



// MARK: - UITabBarControllerDelegate
extension CCTabBarController: UITabBarControllerDelegate
{
    public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
    
    public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        
        ///设置tabbar，带圆角，上下左右都有偏移的距离。注释setupLayer方法里的内容
        //didSelect()
        
    }
}
